#include "Game.h"
#include "Listtexture.h"
Game::Game(void)
{
}

Game::Game(HINSTANCE hInstance, UINT _nCmdShow)
{
	_hInstance = hInstance;
	nCmdShow = _nCmdShow;
	_d3ddv = NULL;
	frameRate = FRAME_RATE;
}

Game::~Game(void){

}

void Game::Init()
{
	cleartrace();
	Global::_Directx->Init(_hInstance);
	Global::Screen.left = 0;
	Global::Screen.top = 0;
	Global::Screen.right = SCREEN_WIDTH;
	Global::Screen.bottom = SCREEN_HEIGHT;
	
	
	Listtexture::CreateAllTexture();
	Global::listObj->Init(Global::maplevel);
	Global::BG->initialize(Global::maplevel);
	Global::SI->Init(20,200);
	Global::binaryTree->ReadBinaryTreeFormFile(Global::maplevel);
	Global::binaryTree->Deserialize();
}

bool Game::Run()
{
	Update();
	Global::_Directx->BeginScene();
	Render();
	Global::_Directx->EndScene();
	return true;
}

void Game::Update()
{
	//Global::RestartMap();
	Global::binaryTree->UpdateScreen();
	Global::UpdateScreen();
}
void Game::Render()
{
	Global::BG->RenderMap(Global::Screen.left,Global::Screen.right);
	Global::binaryTree->RenderScreen();
	Global::SI->RenderSimon();
	//Global::dynamicManager->render();
}

void Game::ProcessKeyboard(){
	Global::_Directx->ProcessKeyBoard();
	for( DWORD i = 0; i < Global::_Directx->dwElements; i++ )
	{
		int keyCode = Global::_Directx->keyEvents[i].dwOfs;
		int keyState = Global::_Directx->keyEvents[i].dwData;
		if ( (keyState & 0x80) > 0)
		{
			OnKeyDown(keyCode);			
		}
		else 
		{
			OnKeyUp(keyCode);
		}
	}
}
void Game::ProcessInput()
{
	Global::SI->ProcessInput();
}
void Game::OnKeyDown(int keyCode){
	Global::SI->OnKeyDown(keyCode);
}
void Game::OnKeyUp(int keyCode){}